package com.example.demo.suanfa_quickSort;

import org.junit.Test;

/**
 * @program: java_base
 * @description: 荷兰国旗
 * @author: zhouhongtao
 * @happyCreateTime: 2022/02/15 09:10
 */
public class NetherlandsFlagDemo {

    /**
     * 入参：
     * 数组
     * 目标分割值
     * 出参：
     * [x,y]
     */
    @Test
    public void netherlandsFlag() {
        int[] arr = {1, 2, 3, 4,32 , 6, 67, 7, 5, 4, 5};
        // 目标值 5
        int target = arr[arr.length-1];
        int l = 0;
        int i = 0;
        int r = arr.length - 1;
        while (i <= r) {
            if (arr[i] < target) {
                swap(arr, l++, i++);
            } else if (arr[i] > target) {
                swap(arr, i, r--);
            } else {
                i++;
            }
        }
        System.out.println(l + ":" + r);
    }

    private void swap(int[] arr, int source, int target) {
        int tmp = arr[source];
        arr[source] = arr[target];
        arr[target] = tmp;
    }


}
